///////////////////////////////////////////////////////////////////////////
/// File: FileHandler.cs
/// Description: 
///    -  本文件是由代码生成工具自动生成的，如非必要，不要修改，若有新增内容，新建一个partial类进行新增。
///    Web处理器，用户界面与服务程序交互的第一个对象
/// Copyright© 2011 Mz
/// History:
///     2011-10-3 14:57:27    My.GenCode    Original Version
///////////////////////////////////////////////////////////////////////////

using System.Collections.Generic;
using System.Web;

using MAF.Common;

namespace My.Blog.Web.Controllers
{
    public partial class FileHandler
    {
        protected IFile _this = BizFactory.Instance.CreateFile();
        protected BasePage _page = null;
        
        // 构造方法
        public FileHandler(BasePage page)
        {
            _page = page;
        }
        
        /// <summary>
        /// CreateFile
        /// </summary>
        public int CreateFile()
        {
            HttpContext context = HttpContext.Current;

            int j = 0;
            for (int i = 0, len = context.Request.Files.Count; i < len; ++i )
            {
                HttpPostedFile file = context.Request.Files[i];
                if (string.IsNullOrEmpty(file.FileName))
                    continue;

                FileInfo info = new FileInfo();
                MAF.Common.ObjectConverter.ToObject<FileInfo>(context.Request.Form, info);

                // 保存文件信息
                info.DisplayName = file.FileName;
                info.FileSize = (int)file.InputStream.Length;
                info.FileType = System.IO.Path.GetExtension(file.FileName).TrimStart('.');

                _this.CreateFile(info, file.InputStream);
                ++j;
            }
            return j;
        }
        
        /// <summary>
        /// CreateFile
        /// </summary>
        public int UpdateFile()
        {
            FileInfo info = new FileInfo();
            MAF.Common.ObjectConverter.ToObject<FileInfo>( HttpContext.Current.Request.Form, info );
            int i = _this.UpdateFile(info);
            return i;
        }
        
        /// <summary>
        /// CreateFile
        /// </summary>
        public int DeleteFiles()
        {
            FileInfo info = new FileInfo();
            info.Ids = HttpContext.Current.Request.Form["Ids"];
            int i = _this.DeleteFiles( info );
            return i;
        }
        
        /// <summary>
        /// CreateFile
        /// </summary>
        public FileInfo ReadFileByConditions()
        {
            FileInfo info = new FileInfo();
            MAF.Common.ObjectConverter.ToObject<FileInfo>( HttpContext.Current.Request.QueryString, info );
            info = _this.ReadFileByConditions( info );
            return info;
        }
        
        /// <summary>
        /// CreateFile
        /// </summary>
        public IList<FileInfo> ReadFilesByConditions(out PageInfo page)
        {
            HttpContext context = HttpContext.Current;
            FileInfo info = new FileInfo();
            MAF.Common.ObjectConverter.ToObject<FileInfo>( context.Request.QueryString, info );
    
            int currPage; int.TryParse(context.Request.QueryString["Page"], out currPage);
            
            page = new PageInfo(_page.PageSize, currPage);
            page.RecordCount = _this.ReadFilesCountByConditions(info);
               
            IList<FileInfo> list = _this.ReadFilesByConditions(info, page.PageSize, page.CurrentPage);
            return list;
        }
    }
}